package cn.bonoon.services;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bonoon.core.ConfigService;
import cn.bonoon.kernel.events.BaseEvent;
import cn.bonoon.kernel.support.services.ServiceSupport;
import cn.bonoon.kernel.web.ConfigManager;

/**
 * <pre>
 * 1.把系统的配置信息和用户的个人配置信息集成为一个表. 
 * 2.把key分为两个部分:type和key,可以支持对配置信息分类,但同一个type下的key必须是唯一的.
 * 3.增加了对大字段的支持(注意:并不是所有的默认支持,在配置类里,那些字段需要用到大字段的,可以使用@Lob来指定)
 * 4.优化类型的转换
 * </pre>
 * 
 * @author jackson
 * 
 */
@Service
@Transactional(readOnly = true)
public class ConfigServiceImpl extends ServiceSupport implements ConfigService {
	
	private ConfigManager configManager;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		configManager = ConfigManager.getManager();
	}

	@Override
	public Object read(BaseEvent event) {
		try {
			return configManager.read(applicationContext, entityManager, event);
		} catch (Exception e) {
			e.printStackTrace();
			return event.getSource();
		}
	}

	@Override
	public Object read(BaseEvent event, Class<?> configClass){
		try {
			return configManager.read(applicationContext, entityManager, event, configClass);
		} catch (Exception e) {
			e.printStackTrace();
			return event.getSource();
		}
	}

	@Override
	@Transactional
	public void write(BaseEvent event) throws Exception{
		configManager.write(applicationContext, entityManager, event);
	}
}
